# -*- coding: utf-8 -*-
from utils.operators.cluster_for_spark_sql_operator import SparkSqlOperator
from datetime import timedelta

from jms.dwd.dwd_warhouse.dwd_wide_sign_trace_waybill_step_dt import jms_dwd__dwd_wide_sign_trace_waybill_step_dt_new
from jms.dwd.tab.dwd_tab_barscan_bagging_base_dt import jms_dwd__dwd_tab_barscan_bagging_base_dt
from jms.dwd.tab.dwd_tab_barscan_send_base_dt import jms_dwd__dwd_tab_barscan_send_base_dt
from jms.dwd.tab.dwd_tab_barscan_arrival_base_dt import jms_dwd__dwd_tab_barscan_arrival_base_dt
from jms.dwd.tab.dwd_tab_barscan_other_base_dt import jms_dwd__dwd_tab_barscan_other_base_dt
from jms.dwd.tab.dwd_second_package_list_base_dt import jms_dwd__dwd_second_package_list_base_dt
from jms.dwd.tab.dwd_tab_barscan_difficult_base_dt import jms_dwd__dwd_tab_barscan_difficult_base_dt
from jms.dwd.tab.dwd_tab_electronic_package_list_base_dt import jms_dwd__dwd_tab_electronic_package_list_base_dt
from jms.dwd.tab.dwd_tab_reback_transfer_express_base_dt import jms_dwd__dwd_tab_reback_transfer_express_base_dt
from jms.dwd.oms.dwd_yl_oms_interceptorpiece_base import jms_dwd__dwd_yl_oms_interceptorpiece_base
from jms.dim.dim_network_whole_massage import jms_dim__dim_network_whole_massage
from jms.dim.dim_lmdm_sys_second_code import jms_dim__dim_lmdm_sys_second_code
from jms.dim.lmdm.dim_yl_sys_package_different_base import jms_dim__dim_yl_sys_package_different_base
from jms.dim.dim_lmdm_sys_first_code import jms_dim__dim_lmdm_sys_first_code
from jms.dwd.oms.dwd_yl_oms_order_return_dt import jms_dwd__dwd_yl_oms_order_return_dt
from jms.dwd.oms.dwd_yl_oms_order_transfer_dt import jms_dwd__dwd_yl_oms_order_transfer_dt
from jms.dwd.oms.dwd_yl_oms_oms_waybill_incre_dt import jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
from jms.dim.dim_yl_mix_package_config_dt import jms_dim__dim_yl_mix_package_config_dt
from jms.time_sensor.time_after_03_30 import time_after_03_30

# --        jms_ods.work_order      普通工单表
# --        jms_ods.project_work_order  平台工单表
# --        jms_dwd.dwd_tab_barscan_collect_base_dt           操作代收表
# --        jms_dwd.dwd_tab_barscan_sign_base_dt           操作签收表
# --        jms_dwd.dwd_tab_reback_transfer_express_base_dt     退转件表
# --        jms_dwd.dwd_yl_oms_oms_order_incre_dt     订单表
# --        jms_dwd.dwd_oms_order_performance_main_base_dt     订单履约表
# --        jms_dim.dim_sys_network_area_base     片区
# --        jms_dim.dim_network_whole_massage     组织表

jms_dm__dm_scan_bagging_true_detail_dt = SparkSqlOperator(
    task_id='jms_dm__dm_scan_bagging_true_detail_dt',
    task_concurrency=1,
    pool_slots=2,
    master='yarn',
    execution_timeout=timedelta(hours=1),
    email=['jokic.wang@jtexpress.com'],
    name='jms_dm__dm_scan_bagging_true_detail_dt_{{ execution_date | date_add(1) | cst_ds }}',
    sql='jms/dm/dm_scan_bagging_true_detail_dt/execute.hql',
    driver_memory='12G',
    driver_cores=1,
    executor_cores=4,
    executor_memory='12G',
    num_executors=50,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={
        'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
        'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
        'spark.dynamicAllocation.maxExecutors': 60,  # 动态资源最大扩容 Executor 数
        'spark.dynamicAllocation.cachedExecutorIdleTimeout': 60,  # 动态资源自动释放闲置 Executor 的超时时间(s)
        'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
        'spark.executor.memoryOverhead': '4G',  # 堆外内存
        'spark.sql.shuffle.partitions': 600,
        'spark.sql.autoBroadcastJoinThreshold': 104857600,  # MapJoin 阈值 100M
        'spark.executor.extraJavaOptions': '-XX:+UseG1GC -XX:ParallelGCThreads=4',
        'spark.yarn.maxAppAttempts': 1,
        'spark.driver.maxResultSize': '10G'
    },
    hiveconf={
       'hive.exec.dynamic.partition': 'true',  # 动态分区
       'hive.exec.dynamic.partition.mode': 'nonstrict',
       'hive.exec.max.dynamic.partitions': 20,  # 每天生成 20 个分区
       'hive.exec.max.dynamic.partitions.pernode': 20  # 每天生成 20 个分区
   },
)

jms_dm__dm_scan_bagging_true_detail_dt << [
    jms_dwd__dwd_wide_sign_trace_waybill_step_dt_new,
    jms_dwd__dwd_tab_barscan_bagging_base_dt,
    jms_dwd__dwd_tab_barscan_send_base_dt,
    jms_dwd__dwd_tab_barscan_arrival_base_dt,
    jms_dwd__dwd_tab_barscan_other_base_dt,
    jms_dwd__dwd_second_package_list_base_dt,
    jms_dwd__dwd_tab_barscan_difficult_base_dt,
    jms_dwd__dwd_tab_electronic_package_list_base_dt,
    jms_dwd__dwd_tab_reback_transfer_express_base_dt,
    jms_dwd__dwd_yl_oms_interceptorpiece_base,
    jms_dim__dim_network_whole_massage,
    jms_dim__dim_lmdm_sys_second_code,
    jms_dim__dim_lmdm_sys_first_code,
    jms_dim__dim_yl_sys_package_different_base,
    jms_dwd__dwd_yl_oms_order_return_dt,
    jms_dwd__dwd_yl_oms_order_transfer_dt,
    jms_dwd__dwd_yl_oms_oms_waybill_incre_dt,
    jms_dim__dim_yl_mix_package_config_dt,
    time_after_03_30
]
